package com.silp.phoneinfo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings.SettingNotFoundException;
import android.telephony.TelephonyManager;
import android.util.Log;

public class Info {

	private Context context;
	private String mstrstate;

	public Info(Context context){
		this.context = context;
	}

	public int Get_BatteryCharge(){
		try 
		{ 
			String line; 
			java.lang.Process p = Runtime.getRuntime().exec("cat sys/class/power_supply/battery/capacity"); 
			BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()),"cat sys/class/power_supply/battery/capacity".length());
			while ((line = input.readLine()) != null) 
			{
				input.close(); 
				return Integer.valueOf( line ).intValue();
			} 
			input.close(); 
		} 
		catch (Exception err) 
		{ 
			Log.e("SILP","ERROR!!!");
		}
		return 0;
	}

	public String Get_NetWork(TelephonyManager tm ){
		if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM)
			return null;
		return tm.getNetworkOperatorName();

	}

	public String Get_IMSI(TelephonyManager tm){
		return tm.getSubscriberId();
	}

	public String Get_IMEI(TelephonyManager tm){
		return tm.getDeviceId();
	}

	public int Get_LAC(TelephonyManager tm){
		android.telephony.gsm.GsmCellLocation cl = (android.telephony.gsm.GsmCellLocation) tm.getCellLocation();
		return cl.getLac();
	}

	public String Get_MCC(TelephonyManager tm){
		if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM)
			return null;
		String no = tm.getNetworkOperator();
		return no.substring(0, 3);
	}

	public String Get_MNC(TelephonyManager tm){
		if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM)
			return null;
		String no = tm.getNetworkOperator();
		return no.substring(3);
	}

	public String Get_ICCID(TelephonyManager tm){
		return tm.getSimSerialNumber();
	}
	public String Get_Number(TelephonyManager tm){
		return tm.getLine1Number();
	}
	public String Get_DataState(TelephonyManager tm){

		int state = tm.getDataState();

		switch (state) {


		case 0:
			mstrstate= "DATA_DISCONNECTED";
			break;
		case 1:
			mstrstate= "DATA_CONNECTING";
			break;
		case 2:
			mstrstate= "DATA_CONNECTED";
			break;
		case 3:
			mstrstate= "DATA_SUSPENDED";
			break;
		default:
			break;
		}
		return mstrstate;

	}	

	public int GetBrightness () {
		int curBrightnessValue = 0;
		try
		{
			curBrightnessValue = android.provider.Settings.System.getInt(context.getContentResolver(),
					android.provider.Settings.System.SCREEN_BRIGHTNESS);
		}
		catch (SettingNotFoundException e){}	
		return curBrightnessValue;
	}
	public String ReadDisk()
	{
		ProcessBuilder cmd;
		String result="";

		try{
			String[] args = {"system/bin/df"};
			cmd = new ProcessBuilder(args);
			Process process = cmd.start();
			InputStream in = process.getInputStream();
			byte[] re = new byte[1024];
			while(in.read(re) != -1){
				result = result + new String(re);
			}
			in.close();
		} catch(IOException ex){
			ex.printStackTrace();
		}
		return result;
	}
	public String ReadCPUinfo()
	{
		ProcessBuilder cmd;
		String result="";

		try{
			String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
			cmd = new ProcessBuilder(args);

			Process process = cmd.start();
			InputStream in = process.getInputStream();
			byte[] re = new byte[1024];
			while(in.read(re) != -1){
				result = result + new String(re);
			}
			in.close();
		} catch(IOException ex){
			ex.printStackTrace();
		}
		return result;
	}
	public String getMemoryInfo(){
		ProcessBuilder cmd;
		String result = new String();
		try{
			String[] args = {"/system/bin/cat", "/proc/meminfo"};
			cmd = new ProcessBuilder(args);
			java.lang.Process process = cmd.start();
			InputStream in = process.getInputStream();
			byte[] re = new byte[1024];
			while(in.read(re) != -1){
				result = result + new String(re);
			}
			in.close();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	public static String getRunningTasksInfo(Context context) {
		StringBuffer sInfo = new StringBuffer();
		final ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasks = activityManager.getRunningTasks(100);
		Iterator<RunningTaskInfo> l = tasks.iterator();
		while (l.hasNext()) {
			RunningTaskInfo ti = (RunningTaskInfo) l.next();
			sInfo.append("PID: ").append(ti.id);
			sInfo.append("\nActivity: ").append(
					ti.baseActivity.flattenToString());
			sInfo.append("\nNum Activities: ").append(ti.numActivities);
			sInfo.append("\nNum Running: ").append(ti.numRunning);
			sInfo.append("\n\n");
		}
		return sInfo.toString();
	}
	public static boolean IsWiFiConnected(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getApplicationContext().getSystemService(
						Context.CONNECTIVITY_SERVICE);

		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getTypeName().equals("WIFI")
							&& info[i].isConnected())
						return true;
				}
			}
		}

		return false;
	}
	public String getWiFiMACAddress(Context context) {
		WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		if (null == wifiManager || null == wifiInfo)
			return "";

		return wifiInfo.getMacAddress();
	}
	public String getWiFiSSID(Context context) {
		WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		if (null == wifiManager || null == wifiInfo)
			return "";

		return wifiInfo.getSSID();
	}
	public String getIPAddress() 
	{
		String ipaddress = "";

		try 
		{
			Enumeration<NetworkInterface> enumnet = NetworkInterface.getNetworkInterfaces();
			NetworkInterface netinterface = null;

			while(enumnet.hasMoreElements()) 
			{
				netinterface = enumnet.nextElement();

				for (Enumeration<InetAddress> enumip = netinterface.getInetAddresses(); 
						enumip.hasMoreElements();) 
				{
					InetAddress inetAddress = enumip.nextElement();

					if(!inetAddress.isLoopbackAddress())
					{
						ipaddress = inetAddress.getHostAddress();

						break;
					}
				}
			}
		} 
		catch (SocketException e) 
		{
			e.printStackTrace();
		}

		return ipaddress;
	}

	public String getWifiIpAddress(Context context){
		WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		{
			if (null == wifiManager || null == wifiInfo)
				return "";

			int ipAddress = wifiInfo.getIpAddress();

			return String.format("%d.%d.%d.%d", 
					(ipAddress & 0xff),
					(ipAddress >> 8 & 0xff), 
					(ipAddress >> 16 & 0xff),
					(ipAddress >> 24 & 0xff));
		}
	}
	public static boolean isNetConnected(Context context) {
		try {
			ConnectivityManager connManger = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if(connManger!=null)
			{
				NetworkInfo netInfo=connManger.getActiveNetworkInfo();
				if(netInfo!=null&&netInfo.isConnected()){
					if(netInfo.getState()==NetworkInfo.State.CONNECTED){
						return true;
					}
				}
			}

		} catch (Exception e) {

		}
		return false;
	}

}


